package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.runtai.entity.InRegardOf;
import com.system.runtai.entity.SecondaryCopywriter;
import com.system.runtai.service.SecondaryCopywriterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 二级文案 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-04-12
 */
@Api(tags = "二级文案")
@RestController
@RequestMapping("/runtai/secondary-copywriter")
public class SecondaryCopywriterController {

    @Autowired
    private SecondaryCopywriterService secondaryCopywriterService;

    @ApiOperation(value = "二级文案")
    @GetMapping("/getSecondaryCopywriterList")
    public Result<List<SecondaryCopywriter>> getInRegardOfList(){
        List<SecondaryCopywriter> list = secondaryCopywriterService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "查看二级文案详情")
    @GetMapping("/getSecondaryCopywriter/{id}")
    public Result<SecondaryCopywriter> getSecondaryCopywriterById(@PathVariable Integer id){
        SecondaryCopywriter byId = secondaryCopywriterService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation(value = "修改二级文案")
    @PostMapping("/updateSecondaryCopywriter/{startId}/{id}")
    public Result<SecondaryCopywriter> updateSecondaryCopywriter(@PathVariable Integer startId,@PathVariable Integer id,@RequestBody SecondaryCopywriter secondaryCopywriter){


        secondaryCopywriter.setUpdateTime(LocalDateTime.now());

        boolean b = secondaryCopywriterService.updateById(secondaryCopywriter);

        if (b){
            return Result.ok(secondaryCopywriter).message("修改成功");
        }
        return Result.fail(secondaryCopywriter).message("修改失败");
    }


    @ApiOperation(value = "添加二级文案")
    @PostMapping("/saveSecondaryCopywriter/{startId}/{id}")
    public Result<SecondaryCopywriter> saveSecondaryCopywriter(@PathVariable Integer startId,@PathVariable Integer id,@RequestBody SecondaryCopywriter secondaryCopywriter){

        SecondaryCopywriter secondaryCopywriter1 = new SecondaryCopywriter();
        secondaryCopywriter1.setCreateTime(LocalDateTime.now());
        secondaryCopywriter1.setStartid(startId);
        secondaryCopywriter1.setMainArgumentId(id);
        secondaryCopywriter1.setTitle(secondaryCopywriter.getTitle());
        secondaryCopywriter1.setPicture(secondaryCopywriter.getPicture());
        secondaryCopywriter1.setArticle(secondaryCopywriter.getArticle());
        boolean save = secondaryCopywriterService.save(secondaryCopywriter1);

        if (save){
            return Result.ok(secondaryCopywriter).message("添加成功");
        }
        return Result.fail(secondaryCopywriter).message("添加失败");
    }

    @ApiOperation(value = "删除二级文案")
    @PostMapping("delectSecondaryCopywriter/{start}/{id}")
    public Result delectSecondaryCopywriter(@PathVariable Integer start,@PathVariable Integer id){

        LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondaryCopywriter::getStartid,start).eq(SecondaryCopywriter::getId,id);
        boolean b = secondaryCopywriterService.remove(wrapper);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "分页查询二级文案")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<SecondaryCopywriter>> indexdavzq(@RequestParam(required = false) Integer startId,@RequestParam(required = false) String title,@RequestParam(required = false) Integer id,@PathVariable Long page, @PathVariable Long limit, SecondaryCopywriter secondaryCopywriter){
        if (StringUtils.isEmpty(title)){


        Page<SecondaryCopywriter> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(SecondaryCopywriter::getStartid,startId).eq(SecondaryCopywriter::getMainArgumentId,id);

        IPage<SecondaryCopywriter> iPage = secondaryCopywriterService.page(recommendPage,wrapper);

        return Result.ok(iPage);
        }else if (!StringUtils.isEmpty(title)){
            Page<SecondaryCopywriter> recommendPage = new Page<>(page,limit);

            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();

            wrapper.eq(SecondaryCopywriter::getStartid,startId).eq(SecondaryCopywriter::getMainArgumentId,id).like(SecondaryCopywriter::getTitle,title);

            IPage<SecondaryCopywriter> iPage = secondaryCopywriterService.page(recommendPage,wrapper);

            return Result.ok(iPage);
        }

        Page<SecondaryCopywriter> recommendPage = new Page<>(page,limit);
        IPage<SecondaryCopywriter> iPage = null;

        return Result.fail(iPage).message("暂无数据");
    }

}

